package com.cjq.core.filter;

import com.cjq.cathe.CacheStore;
import com.cjq.config.GatewayConfig;
import com.cjq.config.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
public class GatewayFilterManger {

    @Autowired
    private ApplicationContext context;

    public final static List<GatewayFilter> filters = new ArrayList();
    private final String filterList;

    public GatewayFilterManger(GatewayConfig config) {
        this.filterList = config.getFilterList();
        initFilter();
    }

    private List<GatewayFilter> initFilter() {
        Map<String, GatewayFilter> gatewayFilterStore = CacheStore.getInstance().getGatewayFilterStore();
        String[] str = filterList.split(",");
        Arrays.stream(str).forEach(str2 -> {
            if (context == null) {
                context = SpringUtil.getApplicationContext();
            }
            GatewayFilter filter = (GatewayFilter) context.getBean(str2);
            gatewayFilterStore.put(str2, (GatewayFilter) context.getBean(str2));
            filters.add(filter);
        });
        return filters;
    }
}